<?php 
namespace app\home\controller;
use think\Controller;
/**
* 公共控制器
*/

class Common extends Controller
{
	
	public function _initialize()
    {   
        if (input('cateid')) {
            $this->getPos(input('cateid'));//调用当前位置
        }
         if(input('aid')){
            $articles=db('article')->field('cateid')->find(input('aid'));
            $cateid=$articles['cateid'];
            $this->getPos($cateid);
        }
        $this->count();
        $this->advert();//调用LED
        $this->conf();//调用配置
       $this->getNavCates();//调用分类,网站导航
    }
    // 获取分类
     public function getNavCates(){
        $cateres=db('category')->where(array('pid'=>0))->select();
        foreach ($cateres as $k => $v) {
            $children=db('category')->where(array('pid'=>$v['cid']))->select();
            // var_dump($children);
            // if($children){
                $cateres[$k]['children']=$children;
            // }else{
                // $cateres[$k]['children']=0;
            // }
        }

        $this->assign('cateres',$cateres);
    }
    // 获取配置
    public function conf(){
        $conf=new \app\home\model\Conf();
        $_confres=$conf->getAllConf();
        $confres=array();
        foreach ($_confres as $k => $v) {
            $confres[$v['enname']]=$v['value'];

        }
        $this->assign('confres',$confres);
    }
    // 当前位置
    public function getPos($cateid){
        $cate=new \app\home\model\Category();
        $posArr=$cate->getparents($cateid);//当前位置分类
        $this->assign('posArr',$posArr);
    }
    // LED
    public function advert(){
        // LED调用
        $advert=db('advert')->order('id desc')->limit(1)->find();
        $this->assign('advert',$advert);
    }
    // 统计访问量
    public function count(){
            ini_set('date.timezone','Asia/Shanghai');
            $date=date('Y-m-d');
            $yesterday=date("Y-m-d",strtotime("-1 day")); 
            $yesterdayc=db('ipcount')->getfieldbynowdate($yesterday,'nowdatec'); 
            $row=db('ipcount')->where('nowdate="'.$date.'"')->select();
            $n=1;
            $add=array(
            'nowdatec'=>$n,
            'nowdate'=>$date,
            );
            if(empty($row)){
                db('ipcount')->insert($add);
            }
                $row1=db('ipcount')->field('nowdatec')->where('nowdate="'.$date.'"')->select();
            foreach($row1 as $cd){
                $dd= $cd['nowdatec'];
            }
                // echo '今天访问量:',$dd,'</br>'; 
                $this->assign('dd',$dd);
                $dd+=1;
                $now=array(
                'nowdatec'=>$dd,
                'nowdate'=>$date,
            );

            db('ipcount')->where('nowdate="'.$date.'"')->update($now);
            $rows=db('ipcount')->field('nowdatec')->select();
            $sun=0; 
            for ($i=0;$i<count($rows);$i++){
                $sun+=$rows[$i]['nowdatec'];
            }

            if(!empty($yesterdayc)){
                $this->assign('yesterdayc',$yesterdayc);
                        // echo '昨天访问量:',$yesterdayc,'</br>';

            }else{
                        // echo '昨天访问量:0</br>';
            }

            if(!empty($sun)){
                $this->assign('sun',$sun);
                         // echo '总访问量:',$sun,'</br>';
            }else{
                        // echo '总访问量:0</br>';
            }


        }



}